package com.lei.ucenter.controller;


import com.lei.ucenter.entity.Member;
import com.lei.ucenter.entity.vo.RegisterVo;
import com.lei.ucenter.service.MemberService;
import com.lei.utils.JwtUtils;
import com.lei.utils.Result;
import com.lei.utils.vo.UcenterMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-13
 */
@RestController
@RequestMapping("/edu/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    //登录接口
    @PostMapping("/login")
    public Result login(@RequestBody Member member){
        String token=memberService.login(member);
        return Result.ok().data("token",token);
    }

    //注册接口
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return Result.ok();
    }


    //根据token获取用户信息
    @GetMapping("/getUserInfo")
    public Result getMemberByToken(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);
        System.out.println(member);
        return Result.ok().data("userInfo",member);
    }





    //根据用户Id获取用户信息
    @GetMapping("getUserById/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id")String id){
        Member member = memberService.getById(id);
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }

    //获取某一天的注册人数
    @GetMapping("countRegister/{day}")
    public Integer countRegister(@PathVariable("day")String day){
        Integer count=memberService.countRegister(day);
        return count;
    }
}

